﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace ConsoleApplication1.Thread3
{
    public class Thread3
    {
        static void Main2()
        {
            ThreadPool.SetMaxThreads(1000, 1000);

            IPAddress ip = IPAddress.Parse("127.0.0.1");
            TcpListener tcpListener = new TcpListener(ip, 500);
            tcpListener.Start();
            while (true)
            {
                ChatCient c = new ChatCient(tcpListener.AcceptTcpClient());
            }
        }
    }

    public class ChatCient
    {
        static TcpClient tcpClient;
        static byte[] byteMesg;
        static string ClientEndPoint;

        public ChatCient(TcpClient tcp)
        {
            tcpClient = tcp;
            byteMesg = new byte[tcp.ReceiveBufferSize];

            //只显示客户端信息
            ClientEndPoint = tcp.Client.RemoteEndPoint.ToString();
            Console.WriteLine("ClientEndPoint is "+ClientEndPoint);

            //获取异步信息
            NetworkStream nws = tcp.GetStream();
            nws.BeginRead(byteMesg, 0, tcp.ReceiveBufferSize, new AsyncCallback(ReceiveAsyncCallback), null);
        }

        //服务端接收信息
        public void ReceiveAsyncCallback(IAsyncResult rst)
        {
            Thread.Sleep(100);
            ThreadPoolMessage("\n Message is Receiving");
            
            //结束异步读取
            NetworkStream nws = tcpClient.GetStream();
            int length = nws.EndRead(rst);
            if (length < 1)
            {
                tcpClient.GetStream().Close();
                throw new Exception("DisConnection");
            }

            //显示接收信息
            string msg = Encoding.UTF8.GetString(byteMesg, 0, length);
            Console.WriteLine("客户端发过来的: "+msg);

            byte[] sendMessage = Encoding.UTF8.GetBytes("Message is Received!");
            NetworkStream networdstreamWrite = tcpClient.GetStream();
            networdstreamWrite.BeginWrite(sendMessage, 0, sendMessage.Length, new AsyncCallback(sendAsynCallBack), null);

        }

        //把信息转换成二进制数据，发送至客户端
        public void sendAsynCallBack(IAsyncResult rst)
        {
            Thread.Sleep(100);
            ThreadPoolMessage("message is Sending");
            //结束异步发送
            tcpClient.GetStream().EndWrite(rst);
            //重新监听
            tcpClient.GetStream().BeginRead(byteMesg, 0, tcpClient.ReceiveBufferSize, new AsyncCallback(ReceiveAsyncCallback), null);
        }

        static void ThreadPoolMessage(string data)
        {
            int a, b;
            ThreadPool.GetAvailableThreads(out a, out b);
            string msg = string.Format("{0}\n  CurrentThreadId is {1}\n  " +
                   "WorkerThreads is:{2}  CompletionPortThreads is :{3}\n",data,Thread.CurrentThread.ManagedThreadId,a.ToString(),b.ToString());
            Console.WriteLine(msg);
        }
    }
}
